package com.zhugang.week13.linkedlist;

/**
 * @program algorithms
 * @description: linkedList
 * @author: chanzhugang
 * @create: 2022/10/21 00:59
 */
public class LinkedList {

    private Node head = null;

    public class Node {
        public int data;
        public Node next;

        public Node(int data, Node next) {
            this.data = data;
            this.next = next;
        }

        public Node() {

        }
    }

    /**
     * 遍历
     *
     * @param head
     */
    public void travel(Node head) {
        Node p = head;
        while (p != null) {
            System.out.println(p.data);
            p = p.next;
        }
    }

    /**
     * 查找
     *
     * @param value
     * @return
     */
    public Node find(int value) {
        // p 指向头节点
        Node p = head;
        while (p != null) {
            if (p.data == value) {
                return p;
            }
            p = p.next;
        }
        return null;
    }

    // 插入：头插、尾插、给定节点之后插入

    /**
     * 头插法
     *
     * @param value
     */
    public void insertAtHead(int value) {
        // 输入的顺序相反，逆序, 反转链表用
        Node newNode = new Node(value, null);
        newNode.next = head;
        head = newNode;
    }

    /**
     * 尾插法1：未优化前
     *
     * @param value
     */
    public void insertAtTail1(int value) {
        Node newNode = new Node(value, null);
        if (head == null) {
            // 优化2：分情况讨论
            head = newNode;
        } else {
            Node p = head;
            while (p.next != null) {
                // 优化1：每次都要遍历，效率慢
                p = p.next;
            }
            // p.next == null，此时p是尾节点
            p.next = newNode;
        }
    }

    /**
     * 针对1优化：添加一个tail节点
     */
    private Node tail;

    public void insertAtTail2(int value) {
        Node newNode = new Node(value, null);
        if (head == null) {
            head = newNode;
            tail = newNode;
        } else {
            // 针对优化1，进行优化
            tail.next = newNode;
            tail = newNode;
        }
    }

    /**
     * 引入一个虚拟头节点 + 尾指针，简化链表尾部插入操作，针对2优化
     *
     * @param value
     */
    private Node dummyHead = new Node();
    private Node tail2 = dummyHead;

    public void insertAtTail3(int value) {
        Node newNode = new Node(value, null);
        // 尾插法
        tail2.next = newNode;
        tail2 = newNode;
    }

    /**
     * 在给定节点后插入
     *
     * @param p
     * @param value
     */
    public void insertAfter(Node p, int value) {
        if (p == null) {
            return;
        }
        Node newNode = new Node(value, null);
        // 先处理新节点的next， 再把新节点拼在给定节点后面
        newNode.next = p.next;
        p.next = newNode;
    }

    // 删除给定节点之后的节点、 删除给定节点(复杂点)

    /**
     * 删除指定节点之后的节点(简单)
     *
     * @param p
     */
    public void deleteNextNode(Node p) {
        // p.next = p.next.next
        if (p == null || p.next == null) {
            return;
        }
        p.next = p.next.next;
    }

    /**
     * 删除给定节点(难一点)
     *
     * @param head
     * @param p
     * @return
     */
    public Node deleteThisNode(Node head, Node p) {
        if (head == null || p == null) {
            return null;
        }
        // 找p的前置节点prev
        Node prev = null;
        Node q = head;
        while (q != null) {
            if (q == p) {
                break;
            }
            prev = q;
            q = q.next;
        }
        if (q == null) {
            // 没有找到p节点，不用处理
            return head;
        }
        // prev是p的前置节点
        if (prev == null) {
            head = head.next;
        } else {
            prev.next = p.next;
        }
        return head;
    }

    /**
     * 删除指定节点p，用虚拟头节点
     *
     * @param head
     * @param p
     * @return
     */
    public Node deleteThisNode2(Node head, Node p) {
        if (head == null || p == null) {
            return null;
        }
        // 虚拟头节点
        Node dummyHead = new Node();
        dummyHead.next = head;
        Node prev = dummyHead;
        Node q = head;
        while (q != null) {
            if (q == p) {
                break;
            }
            prev = q;
            q = q.next;
        }
        if (q == null) {
            // 没有找到p节点，不用操作
            return head;
        }
        // prev是p的前置节点
        prev.next = q.next;
        return dummyHead.next;
    }

}